package com.heimo.agentlicence.htmlchecker;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.heimo.agentlicence.http.HtmlHelper;

public class BasicChecker {
	private final String TAG = "BasicChecker";
	private final String errPattern = "Error 500";

	public boolean hasError(String html) throws Exception {
		if (html == null) {
			Log.e(TAG, "Response is null exception.");
			throw new Exception("Response is null exception.");
		}
		Pattern pattern = Pattern.compile(errPattern);
		Matcher matcher = pattern.matcher(html);
		if (matcher.find()) {
			Log.e(TAG, "Error pattern found : " + errPattern);
			throw new Exception("Error response from server:\n" + errPattern);
		} else {
			return false;
		}
	}

	protected List<NameValuePair> fnFindInputParms(String html)
			throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		HtmlHelper.getAllInputAsNameValuePair(html, nvps);
		return nvps;
	}

	protected List<NameValuePair> fnAddDefaultParams(List<NameValuePair> nvps) {
		for (int i = 0; i < nvps.size(); i++) {
			String name = nvps.get(i).getName();
			if (name.contains("btn")) {
				nvps.add(new BasicNameValuePair("ScriptManager",
						"ScriptManager|" + name));
			}
		}
		nvps.add(new BasicNameValuePair("__ASYNCPOST", "true"));

		int rand = new Random().nextInt(300);
		rand = (rand < 250 ? rand + 100 : rand);
		nvps.add(new BasicNameValuePair("ScrollTop", "" + rand));
		// fnDebug(nvps);
		return nvps;
	}

	protected List<NameValuePair> fnAddExtraParams(List<NameValuePair> nvps) {
		for (int i = 0; i < nvps.size(); i++) {
			NameValuePair nvp = nvps.get(i);
			String name = nvp.getName();
			if (name.contains("btn")) {
				nvps.add(new BasicNameValuePair(
						"dnn$ctr1142$ViewEAALicenceSearch$ddlPeriod", "all"));
			}
//			if (name.equalsIgnoreCase("__dnnVariable")
//					&& nvp.getValue() == null) {
//				nvps.remove(nvp);
//				nvps.add(new BasicNameValuePair("__dnnVariable", variable));
//			}
			if (name.equalsIgnoreCase("ScrollTop")) {
				nvps.remove(nvp);
			}
		}
		return nvps;
	}

	protected void fnDebug(List<NameValuePair> nvps) {
		for (int i = 0; i < nvps.size(); i++) {
			NameValuePair nvp = nvps.get(i);
			System.out.println("==========");
			if (nvp.getName() != null) {
				System.out.println("NAME : " + nvp.getName());
			}
			if (nvp.getValue() != null) {
				System.out.println("VALUE : " + nvp.getValue());
			}
		}
	}
	
	/*
	 * TODO: Error Pattern 1 : 01-11 16:33:22.955:
	 * D/InquireBookingChecker(1045): bookingMadeHtml:Error 500:
	 * java.lang.NullPointerException
	 */

	/*
	 * TODO: Error Pattern 2 : <body bgcolor='#FFFFFF'
	 * onLoad="ermsg('The session is NOT available')">
	 */

	/*
	 * TODO: Error Pattern3 : 01-23 14:02:28.690: I/System.out(8261): <p>The
	 * server is busy, please try again later. </p>
	 */
	private final String variable = "{'__scdoff':'1','dnn_dnnNAV_ctldnnNAV_json':'{nodes:[{id:\'827\',key:\'827\',txt:\'主頁\',ca:\'3\',url:\'http://www.eaa.org.hk/Home_original/tabid/41/language/zh-HK/Default.aspx\',img:\'\',cssIcon:\' \',nodes:[]},{id:\'64\',key:\'64\',txt:\'關於監管局\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/tabid/64/language/zh-HK/Default.aspx\',img:\'\',cssIcon:\' \',nodes:[{id:\'75\',key:\'75\',txt:\'里程碑\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/Milestones/tabid/75/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'76\',key:\'76\',txt:\'抱負及使命\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/Visionandmission/tabid/76/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'77\',key:\'77\',txt:\'主席的話\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/Messagefromchairman/tabid/77/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'78\',key:\'78\',txt:\'監管局成員\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/Membership/tabid/78/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'815\',key:\'815\',txt:\'委員會及小組\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/CommitteesandPanel/tabid/815/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'80\',key:\'80\',txt:\'組織架構\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/Organisationalchart/tabid/80/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'79\',key:\'79\',txt:\'機構管治\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/Corporategovernance/tabid/79/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'81\',key:\'81\',txt:\'服務承諾\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/Performancepledge/tabid/81/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'82\',key:\'82\',txt:\'私隱政策聲明\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/Privacypolicystatement/tabid/82/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'83\',key:\'83\',txt:\'職位空缺\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/Careers/tabid/83/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'84\',key:\'84\',txt:\'投標\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/Tenders/tabid/84/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'85\',key:\'85\',txt:\'公開資料守則\',ca:\'3\',url:\'http://www.eaa.org.hk/AboutEAA/Codeonaccesstoinformation/tabid/85/language/zh-HK/Default.aspx\',img:\'\',nodes:[]}]},{id:\'66\',key:\'66\',txt:\'考試\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/tabid/66/language/zh-HK/Default.aspx\',img:\'\',cssIcon:\' \',nodes:[{id:\'656\',key:\'656\',txt:\'特別通告\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/SpecialAnnouncements/tabid/656/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'800\',key:\'800\',txt:\'2013年資格考試時間表\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/2013Qualifyingexaminationtimetable/tabid/800/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'87\',key:\'87\',txt:\'報名詳情及報名後的事宜\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/Registrationdetailspostregistrationmatters/tabid/87/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'89\',key:\'89\',txt:\'準備資格考試\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/Preparingforqualifyingexaminations/tabid/89/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'683\',key:\'683\',txt:\'考試手冊\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/ExaminationHandbook/tabid/683/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'654\',key:\'654\',txt:\'資格考試內容綱要\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/Qualifyingexaminationssyllabi/tabid/654/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'655\',key:\'655\',txt:\'地產代理及營業員資格考試建議參考資料\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/Suggestedreferencematerialsforexamination/tabid/655/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'88\',key:\'88\',txt:\'資格考試數據\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/Qualifyingexaminationsfigures/tabid/88/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'90\',key:\'90\',txt:\'資格考試結果\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/Qualifyingexaminationsresults/tabid/90/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'658\',key:\'658\',txt:\'資格考試於颱風/惡劣天氣時的安排\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/Typhoonbadweatherarrangementsforexamination/tabid/658/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'709\',key:\'709\',txt:\'提問與解答\',ca:\'3\',url:\'http://www.eaa.org.hk/Examination/FAQs/tabid/709/language/zh-HK/Default.aspx\',img:\'\',nodes:[]}]},{id:\'67\',key:\'67\',txt:\'發牌\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/tabid/67/language/zh-HK/Default.aspx\',img:\'\',cssIcon:\' \',nodes:[{id:\'91\',key:\'91\',txt:\'牌照種類\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/Typesoflicences/tabid/91/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'711\',key:\'711\',txt:\'營業詳情說明書\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/SPOB/tabid/711/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'92\',key:\'92\',txt:\'發牌條件\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/Licensingrequirements/tabid/92/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'129\',key:\'129\',txt:\'地產代理證\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/Estateagentcard/tabid/129/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'93\',key:\'93\',txt:\'牌照費用\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/Licencefees/tabid/93/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'94\',key:\'94\',txt:\'牌照申請及批核程序\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/Applicationprocedure/tabid/94/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'823\',key:\'823\',txt:\'牌照有效期屆滿及續期\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/Licenceexpiryandrenewal/tabid/823/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'705\',key:\'705\',txt:\'如何填寫牌照申請表格\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/Guidetocompletinglicenceapplicationform/tabid/705/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'95\',key:\'95\',txt:\'申請表格\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/Downloadforms/tabid/95/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'96\',key:\'96\',txt:\'牌照目錄\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/Licencelist/tabid/96/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'97\',key:\'97\',txt:\'登記冊\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/Licenceregister/tabid/97/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'98\',key:\'98\',txt:\'專業資格互認\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/Mutualrecognition/tabid/98/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'749\',key:\'749\',txt:\'電子服務\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/eServices/tabid/749/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'99\',key:\'99\',txt:\'提問與解答\',ca:\'3\',url:\'http://www.eaa.org.hk/Licensing/FAQs/tabid/99/language/zh-HK/Default.aspx\',img:\'\',nodes:[]}]},{id:\'68\',key:\'68\',txt:\'規管\',ca:\'3\',url:\'http://www.eaa.org.hk/Compliance/tabid/68/language/zh-HK/Default.aspx\',img:\'\',cssIcon:\' \',nodes:[{id:\'100\',key:\'100\',txt:\'操守守則\',ca:\'3\',url:\'http://www.eaa.org.hk/Compliance/Codeofethics/tabid/100/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'101\',key:\'101\',txt:\'執業通告\',ca:\'3\',url:\'http://www.eaa.org.hk/Compliance/Practicecirculars/tabid/101/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'102\',key:\'102\',txt:\'標準文件\',ca:\'3\',url:\'http://www.eaa.org.hk/Compliance/Prescribedforms/tabid/102/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'103\',key:\'103\',txt:\'地產代理資訊保安及私隱保護政策與指引\',ca:\'3\',url:\'http://www.eaa.org.hk/Compliance/ITsecuritypolicyguidelinepdf/tabid/103/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'104\',key:\'104\',txt:\'第40條的要求及表格\',ca:\'3\',url:\'http://www.eaa.org.hk/Compliance/S40requirementsandforms/tabid/104/language/zh-HK/Default.aspx\',img:\'\',nodes:[]}]},{id:\'69\',key:\'69\',txt:\'投訴\',ca:\'3\',url:\'http://www.eaa.org.hk/Complaint/tabid/69/language/zh-HK/Default.aspx\',img:\'\',cssIcon:\' \',nodes:[{id:\'105\',key:\'105\',txt:\'如何處理投訴\',ca:\'3\',url:\'http://www.eaa.org.hk/Complaint/Stepsofcomplaintinvestigation/tabid/105/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'668\',key:\'668\',txt:\'裁定佣金爭議的機制\',ca:\'3\',url:\'http://www.eaa.org.hk/Complaint/Determinationofcommissiondisputes/tabid/668/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'723\',key:\'723\',txt:\'投訴人須知\',ca:\'3\',url:\'http://www.eaa.org.hk/Complaint/ImportantNoticetoComplainants/tabid/723/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'814\',key:\'814\',txt:\'被投訴人須知\',ca:\'3\',url:\'http://www.eaa.org.hk/Complaint/ImportantNoticetoComplainees/tabid/814/language/zh-HK/Default.aspx\',img:\'\',nodes:[]}]},{bcrumb:\'1\',id:\'70\',key:\'70\',txt:\'紀律處分\',ca:\'3\',url:\'http://www.eaa.org.hk/DisciplinaryProceedings/tabid/70/language/zh-HK/Default.aspx\',img:\'\',cssIcon:\' \',nodes:[{id:\'119\',key:\'119\',txt:\'研訊通知\',ca:\'3\',url:\'http://www.eaa.org.hk/DisciplinaryProceedings/Noticeofinquiryhearings/tabid/119/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'122\',key:\'122\',txt:\'旁聽研訊規則\',ca:\'3\',url:\'http://www.eaa.org.hk/DisciplinaryProceedings/GeneralEtiquetteforInquiryHearings/tabid/122/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{bcrumb:\'1\',selected:\'1\',id:\'659\',key:\'659\',txt:\'搜尋紀律處分紀錄\',ca:\'3\',url:\'http://www.eaa.org.hk/DisciplinaryProceedings/DisciplinaryRecordsSearch/tabid/659/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'748\',key:\'748\',txt:\'研訊於颱風/惡劣天氣時的安排\',ca:\'3\',url:\'http://www.eaa.org.hk/DisciplinaryProceedings/ArrangementsforIHduringTyphoonSevereWeather/tabid/748/language/zh-HK/Default.aspx\',img:\'\',nodes:[]}]},{id:\'71\',key:\'71\',txt:\'持續專業進修計劃\',ca:\'3\',url:\'http://www.eaa.org.hk/CPDScheme/tabid/71/language/zh-HK/Default.aspx\',img:\'\',cssIcon:\' \',nodes:[{id:\'143\',key:\'143\',txt:\'即將舉行的持續專業進修計劃活動\',ca:\'3\',url:\'http://www.eaa.org.hk/CPDScheme/UpcomingCPDActivities/tabid/143/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'676\',key:\'676\',txt:\'地產代理誠信管理計劃\',ca:\'3\',url:\'http://www.eaa.org.hk/CPDScheme/IntegrityManagementProgramme/tabid/676/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'665\',key:\'665\',txt:\'持續專業進修計劃指引 \',ca:\'3\',url:\'http://www.eaa.org.hk/CPDSchemeGuidelines/tabid/116/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'141\',key:\'141\',txt:\'鼓勵措施\',ca:\'3\',url:\'http://www.eaa.org.hk/CPDScheme/IncentivesSubsidies/tabid/141/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'170\',key:\'170\',txt:\'持續專業進修計劃活動評核程序 \',ca:\'3\',url:\'http://www.eaa.org.hk/CPDScheme/AssessmentProceduresforCPDActivity/tabid/170/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'149\',key:\'149\',txt:\'進修問與答\',ca:\'3\',url:\'http://www.eaa.org.hk/CPDScheme/QuestionsandAnswersonCPDScheme/tabid/149/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'675\',key:\'675\',txt:\'持續專業進修活動精選影片重溫\',ca:\'3\',url:\'http://www.eaa.org.hk/CPDScheme/CPDSeminarsHighlights/tabid/675/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'118\',key:\'118\',txt:\'下載專區\',ca:\'3\',url:\'http://www.eaa.org.hk/CPDScheme/DownloadArea/tabid/118/language/zh-HK/Default.aspx\',img:\'\',nodes:[]}]},{id:\'72\',key:\'72\',txt:\'最新消息\',ca:\'3\',url:\'http://www.eaa.org.hk/WhatsNew/tabid/72/language/zh-HK/Default.aspx\',img:\'\',cssIcon:\' \',nodes:[{id:\'123\',key:\'123\',txt:\'新聞稿\',ca:\'3\',url:\'http://www.eaa.org.hk/WhatsNew/Latestpressreleases/tabid/123/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'124\',key:\'124\',txt:\'新執業通告\',ca:\'3\',url:\'http://www.eaa.org.hk/WhatsNew/Newpracticecirculars/tabid/124/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'807\',key:\'807\',txt:\'最新活動\',ca:\'3\',url:\'http://www.eaa.org.hk/WhatsNew/tabid/72/language/zh-HK/Default.aspx\',img:\'\',nodes:[]},{id:\'808\',key:\'808\',txt:\'特別通告\',ca:\'3\',url:\'http://www.eaa.org.hk/WhatsNew/tabid/72/language/zh-HK/Default.aspx\',img:\'\',nodes:[]}]}]}','dnn_dnnNAV_ctldnnNAV_p':''}";

}
